package horsegame;

import javax.imageio.ImageIO;
import javax.swing.*;//让java认识JFrame用来调用
import javax.swing.border.Border;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class horsegame {
    /*
        定义Position数据,以内部类的形式定义
        */
    class Position{

        public String horseInfo;//用于记录马的信息
        public int num;//记录第几匹马
        public int x = 45;//根据x坐标来绘制马这张图片在画布中的位置
        public Position(String horseInfo,int num) {
            this.horseInfo = horseInfo;
            this.num = num;
        }
    }
    class MyPanel extends JPanel{
        private static final long serialVersionUID = 1L;



        private Image image = new ImageIcon("./src/Image/horse.gif").getImage();
        private Image hdoor = new ImageIcon("./src/Image/hdoor.png").getImage();
        private Image vdoor = new ImageIcon("./src/Image/vdoor.png").getImage();

        Position[]pos = new Position[4];
        //对象的初始化，提前把数据先准备好
        public  MyPanel() {
            for (int i = 0; i < pos.length; i++) {
                pos[i] = new Position("第"+(i+1)+"匹马", i+1);
            }
        }


        @Override
        protected void paintComponent(Graphics g) {
            // TODO Auto-generated method stub
            super.paintComponent(g);
            g.setColor(Color.WHITE);//更改面板为白色
            g.fillRect(0,0,1000,800);
            g.setFont(new Font("Segoe Print",Font.BOLD,40));
            int yHeight = 40;
            for (int i = 0; i < pos.length; i++) {
                g.setColor(new Color(20,247,252));//先划数字要更改画笔颜色
                g.drawString(pos[i].num+"", 5, 65+100*i);
                g.setColor(Color.BLACK);
//				g.drawLine(0, 100*(i+1), 1000, 100*(i+1));用黑色划四条线
                g.fillRect(0, 97+100*i, 1000, 3);//矩形
                g.drawImage(image, pos[i].x,100*i,(int)(120*0.8),(int)(130*0.8), this);
                g.drawImage(vdoor,140 , 100*i, null);

            }
            g.setColor(Color.red);
            g.fillRect(950, 0, 3, 400);
        }
    }

    public horsegame() {
        JFrame frame = new JFrame("赛马游戏");//建立一个新的窗口并对其命名
        frame.setSize(1000, 600);//设置窗口大小

        frame.setResizable(false);//不可调整大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//当窗口关闭时使程序结束
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取系统屏幕宽高 或是像素
        frame.setLocation((screenSize.width-1000)/2,(screenSize.height-600)/2);//设置窗口的显示位置
        frame.setLayout(null);//更改布局管理，采用绝对布局

        try {
            Image image = ImageIO.read(new File("./src/Image/ahorse.png"));
            frame.setIconImage(image);
        }catch(IOException e){
            e.printStackTrace();
        }
        //JPanel容器组件，JFrame也属于容器组件 属于顶级组件，可以直接独立显示，JPanel不能独立显示，他需要顶级组件，加入到顶级组件中去
        MyPanel panel = new MyPanel();
        panel.setBounds(0,0 ,1000, 400);
        frame.add(panel);

        JPanel controlPanel = new JPanel();
        controlPanel.setBackground(Color.yellow);
        controlPanel.setBounds(0,400,1000,200);
        controlPanel.setLayout(null);//设置控制面板采用绝对布局 不采用浮动布局
        frame.add(controlPanel);
        JLabel mesg = new JLabel("请选择一匹马");
        mesg.setBounds(5,15,120,20);
        controlPanel.add(mesg);
        mesg.setFont(new Font("微软雅黑",Font.BOLD,15));
        controlPanel.add(mesg);
        ButtonGroup group = new ButtonGroup();
        for (int i = 0; i <4 ; i++) {
            JRadioButton radio = new JRadioButton((i + 1) + "号");
            radio.setFont(new Font("微软雅黑",Font.BOLD,15));
            radio.setBounds(130 + (i * 50), 15, 50, 20);
            radio.setBackground(Color.lightGray);
            group.add(radio);
            controlPanel.add(radio);
        }
        frame.add(controlPanel);
        frame.setVisible(true);//可以在界面中显示,要加在add前 不然无法显示add所加内容

    }

    public static void main(String[] args) {
        new horsegame();
    }
}
